package com.orhon.smartcampus.modules.teacher.fetcher;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLTeacherStatistics;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLTeacherStatisticsTwo;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.dao.UsersDao;
import com.orhon.smartcampus.utils.HttpUtil;
import com.orhon.smartcampus.utils.R;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class TeacherStatisticsFetcher implements DataFetcher {

    @Autowired
    UsersDao usersDao;

    @Autowired
    TokenService tokenService;

    @Override
    @Transactional
    public Object get(DataFetchingEnvironment env) throws Exception {

        String token = tokenService.getSystemToken();
        if(token==null) {
            return  R.NoLogin();
        }
        GQLTeacherStatistics gqlTeacherStatistics = new GQLTeacherStatistics();
//        GQLTeacherStatisticsTwo gqlTeacherStatisticsTwo = new GQLTeacherStatisticsTwo();
        Long schoolId = JwtUtils.getSchoolId(token);
        String schId = "1";
        if (schoolId != null){
            schId = schoolId.toString();
        }
        System.out.println("学校id=================>"+schId);
//        List<Map<String,Object>> genderSta = new ArrayList<>();
//        Map<String,Object> genderMap = new HashMap<>();
        //男教师人员数量
        String query = "{\"query\":{\"bool\":{\"must\":[{\"match\":{\"gender.dictionary_value\":\"gender_1\"}}," +
                "{\"term\":{\"school.id\":\""+schId+"\"}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":10,\"sort\":[],\"aggs\":{}}";
        String s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        JSONObject jsonObject = JSON.parseObject(s);
        Integer man_count = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setMan_count(man_count);
//        genderMap.put("男",man_count);
        //女教师人员数量
        query = "{\"query\":{\"bool\":{\"must\":[{\"match\":{\"gender.dictionary_value\":\"gender_2\"}}," +
                "{\"term\":{\"school.id\":\""+schId+"\"}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":10,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        jsonObject = JSON.parseObject(s);
        Integer woman_count = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setWoman_count(woman_count);
//        genderMap.put("女",woman_count);
        //性别保密人员数量
        query = "{\"query\":{\"bool\":{\"must\":[{\"match\":{\"gender.dictionary_value\":\"gender_0\"}}," +
                "{\"term\":{\"school.id\":\""+schId+"\"}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":10,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        jsonObject = JSON.parseObject(s);
        Integer secrecy_count = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setSecrecy_count(secrecy_count);
//        genderMap.put("保密",secrecy_count);
        //性别未填写
        query = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"school.id\":\""+schId+"\"}}," +
                "{\"match\":{\"gender.dictionary_value\":\"gender_9\"}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":10,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        jsonObject = JSON.parseObject(s);
        Integer notGender = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setNotGender_count(notGender);
//        genderMap.put("未填写",notGender);
//        genderSta.add(genderMap);
//        gqlTeacherStatisticsTwo.setGenderSta(genderSta);
//        List<Map<String,Object>> educationSta = new ArrayList<>();
//        Map<String,Object> educationMap = new HashMap<>();
        //本科学历人员
        query = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"school.id\":\""+schId+"\"}},{\"match\":{\"education.dictionary_value\":\"education_21\"}}]" +
                ",\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":50,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        jsonObject = JSON.parseObject(s);
        Integer bachelor_count = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setBachelor_count(bachelor_count);
//        educationMap.put("本科",bachelor_count);
        //硕士学历人员
        query = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"school.id\":\""+schId+"\"}}," +
                "{\"match\":{\"education.dictionary_value\":\"education_14\"}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":50,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        jsonObject = JSON.parseObject(s);
        Integer master_count = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setMaster_count(master_count);
//        educationMap.put("硕士",master_count);
        //博士学历人员
        query = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"school.id\":\""+schId+"\"}}," +
                "{\"match\":{\"education.dictionary_value\":\"education_11\"}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":50,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        jsonObject = JSON.parseObject(s);
        Integer doctor_count = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setDoctor_count(doctor_count);
//        educationMap.put("博士",doctor_count);
        //其他学历人员
        query = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"school.id\":\""+schId+"\"}}],\"must_not\":[{\"match\":{\"education.dictionary_value\":\"education_21\"}}," +
                "{\"match\":{\"education.dictionary_value\":\"education_14\"}},{\"match\":{\"education.dictionary_value\":\"education_11\"}}],\"should\":[]}},\"from\":0,\"size\":50,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        jsonObject = JSON.parseObject(s);
        Integer other_edu_count = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setOther_edu_count(other_edu_count);
//        educationMap.put("其他",other_edu_count);
//        educationSta.add(educationMap);
//        gqlTeacherStatisticsTwo.setEducationSta(educationSta);
//        List<Map<String,Object>> nationSta = new ArrayList<>();
//        Map<String,Object> nationMap = new HashMap<>();
        //汉族数量
        query = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"school.id\":\""+schId+"\"}}," +
                "{\"match\":{\"nation.dictionary_value\":\"nation_01\"}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":50,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        jsonObject = JSON.parseObject(s);
        Integer han_nation_count = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setHan_nation_count(han_nation_count);
//        nationMap.put("汉族",han_nation_count);
        //蒙族数量
        query = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"school.id\":\""+schId+"\"}}," +
                "{\"match\":{\"nation.dictionary_value\":\"nation_02\"}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":50,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        jsonObject = JSON.parseObject(s);
        Integer meng_nation_count = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setMeng_nation_count(meng_nation_count);
//        nationMap.put("蒙古族",meng_nation_count);
        //回族数量
        query = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"school.id\":\""+schId+"\"}}," +
                "{\"match\":{\"nation.dictionary_value\":\"nation_03\"}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":50,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        jsonObject = JSON.parseObject(s);
        Integer hui_nation_count = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setHui_nation_count(hui_nation_count);
//        nationMap.put("回族",hui_nation_count);
        //藏族数量
        query = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"school.id\":\""+schId+"\"}}," +
                "{\"match\":{\"nation.dictionary_value\":\"nation_04\"}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":50,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        jsonObject = JSON.parseObject(s);
        Integer zang_nation_count = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setZang_nation_count(zang_nation_count);
//        nationMap.put("藏族",zang_nation_count);
        //苗族数量
        query = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"school.id\":\""+schId+"\"}}," +
                "{\"match\":{\"nation.dictionary_value\":\"nation_06\"}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":50,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        jsonObject = JSON.parseObject(s);
        Integer miao_nation_count = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setMiao_nation_count(miao_nation_count);
//        nationMap.put("苗族",miao_nation_count);
        //其他民族数量
        query = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"school.id\":\""+schId+"\"}}]" +
                ",\"must_not\":[{\"match\":{\"nation.dictionary_value\":\"nation_06\"}},{\"match\":{\"nation.dictionary_value\":\"nation_04\"}},{\"match\":{\"nation.dictionary_value\":\"nation_03\"}},{\"match\":{\"nation.dictionary_value\":\"nation_02\"}},{\"match\":{\"nation.dictionary_value\":\"nation_01\"}}],\"should\":[]}},\"from\":0,\"size\":50,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        jsonObject = JSON.parseObject(s);
        Integer other_nation_count = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setOther_nation_count(other_nation_count);
//        nationMap.put("其他",other_nation_count);
//        nationSta.add(nationMap);
//        gqlTeacherStatisticsTwo.setNationSta(nationSta);
//        List<Map<String,Object>> postSta = new ArrayList<>();
//        Map<String,Object> postMap = new HashMap<>();
        //正高级职称人数
        query = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"school.id\":\""+schId+"\"}}," +
                "{\"match\":{\"job_title.dictionary_value\":\"jobtitle_senior\"}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":50,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        jsonObject = JSON.parseObject(s);
        Integer post_high_count = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setPost_high_count(post_high_count);
//        postMap.put("正高级职称",post_high_count);
        //副高级职称人数
        query = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"school.id\":\""+schId+"\"}}," +
                "{\"match\":{\"job_title.dictionary_value\":\"jobtitle_deputy_senior\"}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":50,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        jsonObject = JSON.parseObject(s);
        Integer post_small_high_count = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setPost_small_high_count(post_small_high_count);
//        postMap.put("副高级职称",post_small_high_count);
        //中级职称人数
        query = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"school.id\":\""+schId+"\"}}," +
                "{\"match\":{\"job_title.dictionary_value\":\"jobtitle_intermediate\"}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":50,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        jsonObject = JSON.parseObject(s);
        Integer post_centre_count = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setPost_centre_count(post_centre_count);
//        postMap.put("中级职称",post_centre_count);
        //初级职称人数
        query = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"school.id\":\""+schId+"\"}}," +
                "{\"match\":{\"job_title.dictionary_value\":\"jobtitle_primary\"}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":50,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        jsonObject = JSON.parseObject(s);
        Integer post_primary_count = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setPost_primary_count(post_primary_count);
//        postMap.put("初级职称",post_primary_count);
//        postSta.add(postMap);
//        gqlTeacherStatisticsTwo.setPostSta(postSta);
//        List<Map<String,Object>> workerSta = new ArrayList<>();
//        Map<String,Object> workerMap = new HashMap<>();
        //在职职工人数
        query = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"school.id\":\""+schId+"\"}}," +
                "{\"match\":{\"work_status.dictionary_value\":\"workstatus_on-the-job\"}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":50,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        jsonObject = JSON.parseObject(s);
        Integer on_worker_count = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setOn_worker_count(on_worker_count);
//        workerMap.put("在职职工人数",on_worker_count);
        //退休职工人数
        query = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"school.id\":\""+schId+"\"}}," +
                "{\"match\":{\"work_status.dictionary_value\":\"workstatus_retired\"}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":50,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        jsonObject = JSON.parseObject(s);
        Integer retire_worker_count = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setRetire_worker_count(retire_worker_count);
//        workerMap.put("退休职工人数",retire_worker_count);
        //其他职工人数
        query = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"school.id\":\""+schId+"\"}}]" +
                ",\"must_not\":[{\"match\":{\"work_status.dictionary_value\":\"workstatus_retired\"}},{\"match\":{\"work_status.dictionary_value\":\"workstatus_on-the-job\"}}],\"should\":[]}},\"from\":0,\"size\":50,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        jsonObject = JSON.parseObject(s);
        Integer other_worker_count = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setOther_worker_count(other_worker_count);
//        workerMap.put("其他职工人数",other_worker_count);
        //全部职工人数
        query = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"school.id\":\""+schId+"\"}}]" +
                ",\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":50,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_count", query);
        jsonObject = JSON.parseObject(s);
        Integer all_worker_count = (Integer) jsonObject.get("count");
        gqlTeacherStatistics.setAll_worker_count(all_worker_count);
//        workerMap.put("全部职工人数",all_worker_count);
//        workerSta.add(workerMap);
//        gqlTeacherStatisticsTwo.setWorkerSta(workerSta);
//        List<Map<String,Object>> ageSta = new ArrayList<>();
//        Map<String,Object> ageMap = new HashMap<>();
        //各年龄段教师数量
        query = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"school.id\":\""+schId+"\"}}]" +
                ",\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":2000,\"sort\":[],\"aggs\":{}}";
        s = HttpUtil.sendPost("http://39.104.181.104:9200/smartcampus/teachers/_search", query);
        jsonObject = JSON.parseObject(s);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
        Integer newDate = Integer.valueOf(simpleDateFormat.format(new Date()));
        Integer age_two_two_count = 0;//年龄22-25人数
        Integer age_two_three_count = 0;//年龄25-35人数
        Integer age_three_four_count = 0;//年龄36-45人数
        Integer age_four_six_count = 0;//年龄46-60人数
        Map<String,Object> o = (Map<String, Object>) jsonObject.get("hits");
        if (o != null){
            List<Map<String,Object>> hits = (List<Map<String, Object>>) o.get("hits");
            if (hits != null && hits.size() > 0){
                for (Map<String, Object> hit : hits) {
                    Map<String,Object> source = (Map<String, Object>) hit.get("_source");
                    if (source != null){
                        String idcard = (String) source.get("idcard");
                        if (idcard != null && !"".equals(idcard)){
                            Integer idCardData = Integer.valueOf(idcard.substring(6, 10));
                            if (newDate - idCardData > 22 && newDate - idCardData < 26){
                                age_two_two_count = age_two_two_count + 1;
                            }
                            if (newDate - idCardData > 25 && newDate - idCardData < 36){
                                age_two_three_count = age_two_three_count + 1;
                            }
                            if (newDate - idCardData > 35 && newDate - idCardData < 46){
                                age_three_four_count = age_three_four_count + 1;
                            }
                            if (newDate - idCardData > 45 && newDate - idCardData < 61){
                                age_four_six_count = age_four_six_count + 1;
                            }
                        }
                    }
                }
            }
        }
        gqlTeacherStatistics.setAge_two_two_count(age_two_two_count);
        gqlTeacherStatistics.setAge_two_three_count(age_two_three_count);
        gqlTeacherStatistics.setAge_three_four_count(age_three_four_count);
        gqlTeacherStatistics.setAge_four_six_count(age_four_six_count);
//        ageMap.put("22-25",age_two_two_count);
//        ageMap.put("26-35",age_two_three_count);
//        ageMap.put("36-45",age_three_four_count);
//        ageMap.put("46-60",age_four_six_count);
//        ageSta.add(ageMap);
//        gqlTeacherStatisticsTwo.setAgeSta(ageSta);
        return gqlTeacherStatistics;
    }


}
